/**
 * https://leetcode.cn/problems/max-consecutive-ones-iii/
 * 1004. 最大连续1的个数 III
 * medium, 赖德檀 2024.09.04
 * 滑动窗口
 */
 class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int sum=0,l=0,r=0,len=0;
        while(r<nums.size())
        {
            sum+=nums[r];
            while(r-l+1>sum+k)
            {
                len=max(len,r-l);
                sum-=nums[l++];
            }
            r++;
        }
        len=max(len,r-l);
        return len;
    }
};